package cc.customer.system.service.impl;

import cc.customer.common.domain.Tree;
import cc.customer.common.service.impl.BaseService;
import cc.customer.common.utils.TreeUtils;
import cc.customer.system.dao.CollageMapper;
import cc.customer.system.domain.Collage;
import cc.customer.system.service.CollageService;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("collageService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CollageServiceImpl extends BaseService<Collage> implements CollageService {

    private static final Logger logger = LoggerFactory.getLogger(CollageServiceImpl.class);

    @Override
    public Tree<Collage> getAllCollage() {
        List<Tree<Collage>> list  = new ArrayList<>();
        List<Collage> allCollages = this.getCollageByExample(new Collage());
        allCollages.forEach(collage -> {
            Tree<Collage> tree = new Tree<>();
            tree.setId(collage.getCollageId().toString());
            tree.setParentId(collage.getParentId().toString());
            tree.setText(collage.getCollageName());
            list.add(tree);
        });
        return TreeUtils.build(list);
    }

    @Override
    public List<Collage> getCollageByExample(Collage collage) {
        List<Collage> allCollage = new ArrayList<>();
        try {
            Example example = new Example(Collage.class);
            if (!StringUtils.isEmpty(collage.getCollageName())){
                example.createCriteria().andLike("collageName","%"+collage.getCollageName()+"%");
            }
            example.setOrderByClause("collage_code ");
            allCollage = this.selectByExample(example);
        }catch (Exception e){
            logger.error("获取高校失败");
            e.printStackTrace();
        }
        return allCollage;
    }

    @Override
    public List<Collage> getCollageByParentId(Collage collage) {
        List<Collage> allCollage = new ArrayList<>();
        try {
            Example example = new Example(Collage.class);
            if (!StringUtils.isEmpty(collage.getParentId()+"")){
                Example.Criteria criteria=  example.createCriteria();
                criteria.andCondition("parent_id=",collage.getParentId());
            }
            allCollage = this.selectByExample(example);
        }catch (Exception e){
            logger.error("获取高校失败");
            e.printStackTrace();
        }
        return allCollage;
    }

    @Override
    public List<Collage> getAllCollageByExample(Collage collage) {
        List<Collage> allCollage = new ArrayList<>();
        try {
            Example example = new Example(Collage.class);
            Example.Criteria criteria=  example.createCriteria();
            criteria.andCondition("parent_id=",0);
            if (!StringUtils.isEmpty(collage.getCollageName())){
                criteria.andLike("collageName","%"+collage.getCollageName()+"%");
            }
            allCollage = this.selectByExample(example);
        }catch (Exception e){
            logger.error("获取高校失败");
            e.printStackTrace();
        }
        return allCollage;
    }

    @Override
    @Transactional
    public void deleteCollage(String collageId) {
        try {
            List<String> list = Arrays.asList(collageId.split(","));
             this.batchDelete(list,"collageId",Collage.class);
        }catch (Exception e){

        }
    }

    @Override
    @Transactional
    public void addCollage(Collage collage) {
        Long parentid = collage.getParentId();
        if (parentid == null){
            collage.setParentId(0L);
        }
        this.save(collage);
    }

    @Override
    public Collage getCollageByKey(Long CollageId) {
        Collage collage1  = this.selectByKey(CollageId);
        return collage1;
    }

    @Override
    @Transactional
    public void updateCollage(Collage collage) { this.updateNotNull(collage); }

    /**
     * 获取高校的专业
     * **/
    @Override
    public List<Collage> getCollagePref(Long collageId) {
        List<Collage> allCollage = new ArrayList<>();
        try {
            Example example = new Example(Collage.class);
            Example.Criteria criteria=  example.createCriteria();
            criteria.andCondition("parent_id=",collageId);
            example.setOrderByClause("collage_code ");
            allCollage = this.selectByExample(example);
        }catch (Exception e){
            logger.error("获取高校失败");
            e.printStackTrace();
        }
        return allCollage;
    }
}
